package com.init.iblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.init.iblog.constants.SystemConstants;
import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.dto.CategoryDto;
import com.init.iblog.domain.entity.Blog;
import com.init.iblog.domain.entity.Category;
import com.init.iblog.domain.vo.CategoryVo;
import com.init.iblog.domain.vo.PageVo;
import com.init.iblog.enums.AppHttpCodeEnum;
import com.init.iblog.exception.SystemException;
import com.init.iblog.mapper.CategoryMapper;
import com.init.iblog.service.BlogService;
import com.init.iblog.service.CategoryService;
import com.init.iblog.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2023-02-18 11:09:53
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private BlogService blogService;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 获取文章分类
     * @return
     */
    @Override
    public ResponseResult getCategoryList() {
        //查询博客表 状态为已发布的博客
        LambdaQueryWrapper<Blog> blogWrapper = new LambdaQueryWrapper<>();
        blogWrapper.eq(Blog::getStatus, SystemConstants.BLOG_STATUS_NORMAL);
        List<Blog> blogList = blogService.list(blogWrapper);

        //获取文章的分类id，并去重
        Set<Long> categoryIds = blogList.stream()     //将单列集合转化为stream流
                .map(blog -> blog.getCategoryId())   //将Blog类型转化为Long类型
                .collect(Collectors.toSet());//toSet()方法可将重复的数据去重，并收集到Collectors中

        //查询分类表:通过去重后的文章分类id去Category表中查询分类
        List<Category> categories = listByIds(categoryIds);
        categories = categories.stream()
                .filter((category ->SystemConstants.STATUS_NORMAL.equals(category.getStatus())))
                .collect(Collectors.toList());

        // 封装vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        System.out.println(categoryVos.toString());
        return ResponseResult.okResult(categoryVos);
    }


    /**
     * 查询所有分类接口
     * @return
     */
    @Override
    public List<CategoryVo> listAllCategory() {

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemConstants.NORMAL);
        List<Category> list = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return categoryVos;
    }

    @Override
    public ResponseResult<PageVo> pageCategoryList(Integer pageNum, Integer pageSize, CategoryDto categoryDto) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(categoryDto.getName()),Category::getName,categoryDto.getName());
        queryWrapper.eq(StringUtils.hasText(categoryDto.getStatus()),Category::getStatus,categoryDto.getStatus());
        Page<Category> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        page(page,queryWrapper);
        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addCategory(Category category) {

        if(!StringUtils.hasText(category.getName())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateCategory(Category category) {

        categoryMapper.updateById(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteCategory(List<Long> ids) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        for(Long id: ids){
            categoryMapper.deleteById(id);
        }

        return ResponseResult.okResult();
    }


}
